package org.lyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;

import org.lyg.dao.IUserMapper;
import org.lyg.domain.User;
import org.lyg.domain.UserCondition;
import org.lyg.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements IUserService {
    @Autowired
    IUserMapper userMapper;

    @Override
    public PageInfo<User> list(int page, int size, UserCondition condition) {
        PageHelper.startPage(page,size);
        PageInfo<User> userPageInfo = new PageInfo<>(userMapper.list(condition));
        return userPageInfo;
    }

    @Override
    public boolean save(User user) {
        return userMapper.save(user) > 0;
    }

    @Override
    public boolean delete(int id) {
        return userMapper.delete(id) > 0;
    }

    @Override
    public boolean update(User user) {
        return userMapper.update(user) > 0;
    }

    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

    @Override
    public boolean login(User user) {
        if (StringUtils.isNullOrEmpty(user.getPwd())) {
            return false;
        }
        if (user.getId() == -1) {
            return false;
        }
        User u = userMapper.login(user);
        String token = UUID.randomUUID().toString();

        if (u!=null){
        }
        return u != null;
    }

    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }
}
